#!/bin/bash
set -e

SHELLDIR="$(dirname "$(readlink -f "$0")")"
cd "$SHELLDIR"

execute_script(){
  # $1 script
  if [ -x "$1" ]; then
    "$1" "  Execute: $1"
  fi
}

install_package_cfg(){
  # $1 package
  package="$1"
  echo "Install configure: $package"
  [ "$UID" == "0" ] && execute_script "$SHELLDIR/packages/$package/system.sh"
  execute_script "$SHELLDIR/packages/$package/user.sh"
}

main(){
  # 读取配置
  source ./env.sh

  # 规则设置
  rule="$1"
  [ "$rule" ] || rule=normal
  rule_path="rules/$rule"
  [ ! -f "rules/$rule" -a -f "$rule" ] && rule_path="$rule"

  # 安装规则
  for class in $(cat "$rule_path"); do
	for pkg in $(ls -1 "class/$class"); do
	  install_package_cfg "$pkg"
	done
  done
}

main "$@"

